package com.gui.utils.FrameTest;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

/**
 * @Description:
 * @Author: yangzhiwei
 * @Date: 2021/8/19 14:17
 */
public class MyFrame extends Frame {
   public MyFrame() {
       TextField src1 = new TextField(10);
       src1.setName("test");
       //src1.setName();
       JDialog dialog = new JDialog(this);


       //Event.ACTION_EVENT
       src1.addFocusListener(new FocusListener() {
           @Override
           public void focusGained(FocusEvent e) {
               System.out.println("focusGained");
               dialog.setTitle("输入整型");
                dialog.addNotify();
           }

           @Override
           public void focusLost(FocusEvent e) {
               System.out.println("focusLost");
                try {
                    Integer.valueOf(src1.getText());
                } catch (RuntimeException e1) {
                    System.out.println("不是整型");
                   // dialog.
                }
           }
       });
       src1.addActionListener((e) ->{
        //   System.out.println(1);
           System.out.println("modify" + e.getModifiers());
           System.out.println("command" +  e.getActionCommand());
       });

       TextField cal = new TextField(5);

       TextField src2 = new TextField(10);

       TextField res = new TextField(10);

       Button calcualte = new Button("calculate");
       Dimension dimension = new Dimension();
       dimension.setSize(2,3);
       calcualte.setSize(dimension);
       calcualte.addActionListener((e) ->{
          int li = Integer.valueOf(src1.getText());
          int i2 = Integer.valueOf(src2.getText());
          String calTag = cal.getText();
           switch (calTag) {
               case "+":
                    res.setText(""+(li +i2));
                    break;
               case "-":
                   res.setText(""+(li - i2));
                    break;
                case "*":
                   res.setText(""+(li * i2));
                   break;
               case "/":
                   res.setText(""+(li / i2));
                   break;
           }
       });

       GridLayout gridLayout = new GridLayout(1, 5,50, 50);
       setLayout(gridLayout);
       add(src1);
       add(cal);
       add(src2);
       add(res);
        add(calcualte);
       setVisible(true);
       pack();


   }
}
